# 二进制加减法


# 方法1：转成十进制进行加减法计算然后将接如果转成二进制
def addBinary(self, a: str, b: str) -> str:
    # 转成十进制加减法计算，结果再转成二进制
    return '{0:b}'.format(int(a, 2) + int(b, 2))


# 像十进制加减法一样，冯二进一
def addBinary1(a, b):
    i = len(a) - 1
    j = len(b) - 1
    car = 0
    resList = []
    while i >= 0 and j >= 0:
        resList.append(str((int(a[i]) + int(b[j]) + car) % 2))
        car = (int(a[i]) + int(b[j]) + car) // 2
        i = i - 1
        j = j - 1
        print(resList)
    while i >= 0:
        resList.append(str((int(a[i]) + car) % 2))
        car = (int(a[i]) + car) // 2
        i = i - 1
        print(resList)
    while j >= 0:
        resList.append(str((int(b[j]) + car) % 2))
        car = (int(b[j]) + car) // 2
        j = j - 1
        print(resList)
    if car > 0:
        resList.append('1')
    resList.reverse()
    return ''.join(resList)

if __name__ == '__main__':
    print(addBinary1('111','1'))